package net.maergaiyun.controller;

import net.maergaiyun.controller.request.*;
import net.maergaiyun.enums.BizCodeEnum;
import net.maergaiyun.mapper.VisitStatsMapper;
import net.maergaiyun.service.VisitStatsService;
import net.maergaiyun.util.JsonData;
import net.maergaiyun.vo.VisitStatsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/visit_stats/v1")
public class VisitStatsController {

    @Autowired
    private VisitStatsService visitStatsService;


    @RequestMapping("page_record")
    public JsonData pageVisitRecord(@RequestBody VisitRecordPageRequest request) {

        int total = request.getSize() * request.getPage();

        if (total > 1000) {
            return JsonData.buildResult(BizCodeEnum.DATA_OUT_OF_LIMIT_SIZE);
        }

        Map<String, Object> pageResult = visitStatsService.pageVisitRecord(request);
        return JsonData.buildSuccess(pageResult);
    }

    @RequestMapping("region_day")
    public JsonData queryRegionWithDay(@RequestBody RegionQueryRequest request) {
        List<VisitStatsVO> visitStatsVOList = visitStatsService.queryRegionWithDay(request);
        return JsonData.buildSuccess(visitStatsVOList);
    }

    @RequestMapping("trend")
    public JsonData queryVisitTrend(@RequestBody VisitTrendQueryRequest request) {
        List<VisitStatsVO> visitStatsVOList = visitStatsService.queryVisitTrend(request);
        return JsonData.buildSuccess(visitStatsVOList);
    }

    @RequestMapping("frequent_source")
    public JsonData queryFrequentSource(@RequestBody FrequentSourceRequest request) {
        List<VisitStatsVO> visitStatsVOList = visitStatsService.queryFrequentSource(request);
        return JsonData.buildSuccess(visitStatsVOList);
    }

    @RequestMapping("device_info")
    public JsonData queryDeviceInfo(@RequestBody DeviceQueryRequest request) {
        Map<String, Object> map = visitStatsService.queryDeviceInfo(request);
        return JsonData.buildSuccess(map);
    }
}



